<template>
	<view class="main">
		<view class="main_resume_head">
			<image :src="imgURL+ info.logo" ></image>
		</view>
		<view style="display: flex;justify-content: center;">
			<view class="main_resume_content">
				<view class="main_resume_content_right">
					<view>
						<view class="main_resume_title" style="display: flex; justify-content: space-between;align-items: center;">
							<view>企业信息</view> 
							<view style="color: blue;font-size: 32rpx;" @click="goto('/pages/enterprise/basic?edit=1')">修改</view>
						</view>
						<view class="main_resume_box">
							<view class="main_resume_box_job">
								<view class="main_resume_box_job_time">
									<view v-if="!info.openid" style="color: blue;font-size: 32rpx;" @tap="wxLogin">绑定微信</view>
								</view>
								<view class="main_resume_box_job_time">
									<u-icon
										size="46rpx"
										name="home"
										style="margin-right: 20rpx;"
									></u-icon>
									{{info.ent_name || ""}}
								</view>
								<view class="main_resume_box_job_time">
									<u-icon
										size="46rpx"
										name="coupon"
										style="margin-right: 20rpx;"
									></u-icon>
									{{info.fieldname || ""}}
								</view>
								<view class="main_resume_box_job_time">
									<u-icon
										size="46rpx"
										name="integral"
										style="margin-right: 20rpx;"
									></u-icon>
									{{info.scale || ""}}
								</view>
								<view class="main_resume_box_job_time">
									<u-icon
										size="46rpx"
										name="phone"
										style="margin-right: 20rpx;"
									></u-icon>
									{{info.phone || ""}}
								</view>
								<view class="main_resume_box_job_time">
									<u-icon
										size="46rpx"
										name="map"
										style="margin-right: 20rpx;"
									></u-icon>
									{{info.address || ""}}
								</view>
								<view class="main_resume_box_job_intro">
									{{info.about || ""}}
								</view>
							</view>
						</view>
					</view>
					<view style="margin-top:60rpx">
						<view class="main_resume_title" style="display: flex; justify-content: space-between;align-items: center;">
							<view>视频展示</view> 
							<view style="color: blue;font-size: 32rpx;" @click="goto('/pages/enterprise/video')">{{info.video ? "修改" : "添加"}}</view>
						</view>
						<view class="main_resume_box">
							<view class="main_resume_box_job">
								<view v-if="info.video">
									<video style="width: 650rpx;height: 300rpx;" :src="imgURL+info.video"></video>
								</view>
							</view>
						</view>
					</view>
					<view style="margin-top:60rpx">
						<view class="main_resume_title" style="display: flex; justify-content: space-between;align-items: center;">
							<view>办公环境</view> 
							<view style="color: blue;font-size: 32rpx;" @click="goto('/pages/enterprise/image')">{{imgs.length > 0 ? "修改" : "添加"}}</view>
						</view>
						<view class="main_resume_box">
							<view class="main_resume_box_job" style="display: flex;">
								<view v-if="imgs">
									<block v-for="(item,index) in imgs">
										<image @click="shoImg(index)" style="width: 170rpx;height:170rpx;margin-right: 20rpx;" :src="imgURL + item"></image>
									</block>
								</view>
							</view>
						</view>
					</view>
					<view style="margin-top:60rpx">
						<view class="main_resume_title">
							<view>公司福利</view> 
						</view>
						<view class="main_resume_box">
							<view class="main_resume_box_job" style="display: flex;flex-wrap: wrap;" v-if="info.welfarelist">
								<block v-for="(item,index) in info.welfarelist" >
									<view class="main_box_label">
										{{item.name}}
									</view>
								</block>
							</view>
						</view>
						<view class="main_box">
							<view class="main_box_job">
								<view class="main_box_job_intro">
									{{info.remark || ""}}
								</view>
							</view>
						</view>
					</view>
					<view style="margin-top:60rpx">
						<view class="main_resume_title" style="display: flex; justify-content: space-between;align-items: center;">
							<view>招聘职位</view> 
							<view style="color: blue;font-size: 32rpx;" @click="openmodel">添加</view>
						</view>
						<block v-for="(item,index) in info.position">
							<view class="main_resume_box">
								<view class="main_resume_box_job_left">
									<image style="width:50rpx; height:50rpx" src="../../static/img/jobbao.png"></image> 
								</view>
								<view class="main_resume_box_job">
									<view class="main_resume_box_job_company" style="display: flex; justify-content: space-between;align-items: center;">
										<view>{{item.position || ""}}</view>
										<view style="display: flex;width: 150rpx;justify-content: space-between;">
											<view style="color: blue;font-size: 32rpx;" @click="openmodel(item.position_id)">修改</view>
											<view style="color: blue;font-size: 32rpx;" @click="delPosition(item.position_id)">删除</view>
										</view>
									</view>
									<view class="main_resume_box_job_time">薪资：{{item.salary_low || ""}}-{{item.salary_hei || ""}}</view>
									<view class="main_resume_box_job_post">技能：{{item.skill || ""}}</view>
									<view class="main_resume_box_job_intro">
										{{item.introduce || ""}}
									</view>
								</view>
							</view>
						</block>
					</view>
				</view>
				</view>
		</view>
		<Tabbar :count="2"></Tabbar>
		<u-modal
			title="添加招聘岗位"
			:show="showModal"
			showCancelButton
			asyncClose
			@confirm="savePosition"
			@cancel="() => showModal = false"
		>
			<u--form
				labelPosition="left"
				:model="positionform"
				ref="form"
			>
				<u-form-item
					label="招聘岗位"
					prop="companyName"
					borderBottom
					labelWidth="80"
				>
					<u--input
						v-model="positionform.position"
						border="none"
						placeholder="招聘岗位"
					></u--input>
				</u-form-item>
				<u-form-item
					label="岗位薪资"
					prop="salary"
					borderBottom
					labelWidth="80"
					@click="showPicker"
				>
					<u--input
						v-model="positionform.salary"
						border="none"
						placeholder="岗位薪资"
					></u--input>
					<u-icon
						slot="right"
						name="arrow-right"
					></u-icon>
				</u-form-item>
				<u-form-item
					label="工作经验"
					prop="work_y"
					borderBottom
					labelWidth="80"
					@click="showWork = true; hideKeyboard()"
				>
					<u--input
						v-model="positionform.work_y"
						disabled
						border="none"
						placeholder="工作经验"
					></u--input>
					<u-icon
						slot="right"
						name="arrow-right"
					></u-icon>
				</u-form-item>
				<u-form-item
					label="学历"
					prop="education"
					borderBottom
					labelWidth="80"
					@click="showEducation = true; hideKeyboard()"
				>
					<u--input
						v-model="positionform.education"
						disabled
						border="none"
						placeholder="学历,例如:本科"
					></u--input>
					<u-icon
						slot="right"
						name="arrow-right"
					></u-icon>
				</u-form-item>
				<u-form-item
					label="性别"
					prop="sex"
					borderBottom
					labelWidth="80"
					@click="showSex = true; hideKeyboard()"
				>
					<u--input
						v-model="positionform.sex"
						disabled
						placeholder="请选择性别"
						border="none"
					></u--input>
					<u-icon
						slot="right"
						name="arrow-right"
					></u-icon>
				</u-form-item>
				<u-form-item
					label="招聘人数"
					prop="num"
					borderBottom
					labelWidth="80"
				>
					<u--input
						v-model="positionform.num"
						border="none"
						placeholder="招聘人数"
					></u--input>
				</u-form-item>
				<u-form-item
					label="职业技能"
					prop="startDate"
					borderBottom
					labelWidth="80"
				>
					<u--input
						v-model="positionform.skill"
						border="none"
						placeholder="职业技能"
					></u--input>
				</u-form-item>
				<u-form-item
					label="岗位介绍"
					prop="introduce"
					labelWidth="80"
				>
					<u--textarea
						placeholder="不低于3个字"
						v-model="positionform.introduce"
						count
					></u--textarea>
				</u-form-item>
			</u--form>
		</u-modal>
		<u-picker
			:show="showSalary"
			:columns="salarylist"
			keyName="salary"
			closeOnClickOverlay
			@cancel="cancel"
			@confirm="PickerChange"
			@close="close"
		></u-picker>
		
		<u-action-sheet
			:show="showWork"
			:actions="worklist"
			title="请选择工作经验"
			description="请选择"
			@close="showWork = false"
			@select="workSelect"
		>
		</u-action-sheet>
		<u-action-sheet
			:show="showSex"
			:actions="actions"
			title="请选择性别"
			description="请选择"
			@close="showSex = false"
			@select="sexSelect"
		>
		</u-action-sheet>
		<u-action-sheet
			:show="showEducation"
			:actions="educationlist"
			title="请选择学历"
			description="请选择"
			@close="showEducation = false"
			@select="educationSelect"
		>
		</u-action-sheet>
	</view>
</template>

<script>
	import Tabbar from "@/components/entTabbar"
	import videoPlayer from '@/components/video'
	import { myenterprise,getJobSalary } from '@/api/enterprise.js'
	import { getPositionInfo,savePosition,delPosition } from '@/api/position.js'
	import { imgURL } from '@/utils/config'
	import util from "@/utils/utils.js"
	import {wechatAuth} from '@/api/user.js'
	export default {
		components: {
			Tabbar,
			videoPlayer
		},
		data() {
			return {
				showSalary:false,
				salarylist:[[],[]],
				imgURL:imgURL,
				info: '',
				height:0,
				imgs:[],
				showModal:false,
				positionform:{
					position_id:'',
					position:'',// varchar(255) NOT NULL DEFAULT '' COMMENT '职位',
					salary:'',
					salary_low:'', // `` varchar(255) NOT NULL DEFAULT '' COMMENT '薪资',
					salary_hei:'',//  `` varchar(255) NOT NULL DEFAULT '',
					introduce:'',//  `` text NOT NULL COMMENT '职位介绍',
					skill:'',//  `` char(50) NOT NULL DEFAULT '0' COMMENT '技能',
					education:'',//学历要求
					num:'',//招聘人数
					sex:'',
					work_y:''
				},
				showWork:false,
				worklist:[{
					name: '应届生',
				},
				{
					name: '1-3年',
				},
				{
					name: '3-5年',
				},],
				showSex:false,
				actions: [{
						name: '男',
					},
					{
						name: '女',
					},
					{
						name: '男女不限',
					},
				],
				showEducation:false,
				educationlist:[
					{
						name: '初中',
					},
					{
						name: '中专',
					},
					{
						name: '高中',
					},
					{
						name: '大专',
					},
					{
						name: '本科',
					},
					{
						name: '硕士',
					},
					{
						name: '博士',
					},
				],
				userInfo:{
					encryptedData:'',
					iv:'',
					code:'',
				}
			}
		},
		onLoad() {
			let that = this
			 uni.getSystemInfo({
					success: (res)=> {
						that.height = res.windowHeight- uni.upx2px(100)-260 //获取系统信息，可使用窗口的高度
					}
			});
			this.getEnterprise()
		},
		methods: {
			//获取信息
			getEnterprise() {
				myenterprise({}).then(res=>{
					if(res.code == 200){
						this.info = res.data
						if(res.data.imgs){
							this.imgs = JSON.parse(res.data.imgs)
						}
					}
					else{
						uni.redirectTo({
							url: '/pages/enterprise/basic'
						});
					}
				})
			},
			
			
			openmodel(position_id) {
				this.showModal = true
				
				this.salarylist = []
				getJobSalary({}).then((res)=>{
					if(res.code == 200){
						this.salarylist.push(res.data)
						this.salarylist.push(res.data)
					}
				})
				
				if(position_id){
					getPositionInfo({position_id:position_id}).then((res)=>{
						this.positionform.position_id = res.data.position_id
						this.positionform.position = res.data.position// varchar(255) NOT NULL DEFAULT '' COMMENT '职位',
						this.positionform.salary_low = res.data.salary_low // `` varchar(255) NOT NULL DEFAULT '' COMMENT '薪资',
						this.positionform.salary_hei = res.data.salary_hei//  `` varchar(255) NOT NULL DEFAULT '',
						this.positionform.introduce = res.data.introduce//  `` text NOT NULL COMMENT '职位介绍',
						this.positionform.skill = res.data.skill//  `` char(50) NOT NULL DEFAULT '0' COMMENT '技能',
						this.positionform.education = res.data.education//学历要求
						this.positionform.num = res.data.num//招聘人数
						this.positionform.sex = res.data.sex
						this.positionform.work_y = res.data.work_y
						this.positionform.salary = this.positionform.salary_low +"-"+this.positionform.salary_hei
					})
				}
			},
			
			//选择性别
			sexSelect(e) {
				this.positionform.sex = e.name
			},
			
			//工龄
			workSelect(e) {
				this.positionform.work_y = e.name
			},
			//选择学历
			educationSelect(e) {
				this.positionform.education = e.name
			},
			//删除
			delPosition(position_id) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					success: function (res) {
						if (res.confirm) {
							delPosition({position_id:position_id}).then((res)=>{
								uni.showToast({
									title:res.msg,
									duration: 2000,
									icon:'none',
									success() {
										that.getEnterprise()
									}
								});
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			
			
			shoImg(index){
				var imgbox = [];
				for(let i in this.imgs){
					imgbox.push(this.imgURL+this.imgs[i])
				}
				uni.previewImage({
					//当前显示图片
					current: imgbox[index],
					//所有图片
					urls: imgbox
				})
				//util.previewImage(index)
			},
			
			//showPicker
			showPicker(){
				this.showposition = true
			},
			//选择领域
			PickerChange(e){
				this.positionform.salary_low = e.value[0].salary
				this.positionform.salary_hei = e.value[1].salary
				this.positionform.salary = e.value[0].salary+"-"+e.value[1].salary
				this.showSalary = false
			},
			//薪资弹窗
			showPicker() {
				this.showSalary = true
			},
			//点击按钮关闭薪资弹窗
			close() {
				this.showSalary = false
			},
			//点击遮罩层关闭薪资弹窗
			cancel() {
				this.showSalary = false
			},
			
			//发布工作岗位
			savePosition(){
				let that = this
				if (this.positionform.position == "") {
					return uni.showToast({
						icon: 'none',
						title: '请填写招聘岗位'
					});
				}
				if (this.positionform.salary == "") {
					return uni.showToast({
						icon: 'none',
						title: '请选择薪资待遇'
					});
				}
				if (this.positionform.work_y == "") {
					return uni.showToast({
						icon: 'none',
						title: '请选择工作经验'
					});
				}
				if (this.positionform.education == "") {
					return uni.showToast({
						icon: 'none',
						title: '请选择学历'
					});
				}
				if (this.positionform.sex == "") {
					return uni.showToast({
						icon: 'none',
						title: '请选择招聘性别'
					});
				}
				if (this.positionform.num == "") {
					return uni.showToast({
						icon: 'none',
						title: '请填写招聘人数'
					});
				}
				if (this.positionform.skill == "") {
					return uni.showToast({
						icon: 'none',
						title: '请填写岗位所需技能'
					});
				}
				if (this.positionform.introduce == "") {
					return uni.showToast({
						icon: 'none',
						title: '请填写岗位描述和介绍'
					});
				}
				savePosition(this.positionform).then(res=>{
					if(res.code == 200){
						that.showModal = false
						uni.showToast({
							title:res.msg,
							duration: 2000,
							icon:'none',
							success() {
								that.getEnterprise()
							}
						});
					}
					else{
						return uni.showToast({
							icon: 'none',
							title: "提交失败"
						});
					}
				})
			},
			
			// 新增图片
			async upload() {
				let _this = this;
				util.uploadFile("/user/Upload").then((res)=>{
					if(res.code == 200){
						uni.showToast({
							title:res.msg,
							duration: 2000,
							icon:'none',
							success() {
								_this.form.avatar = res.data
							}
						});
					}
					else{
						return uni.showToast({
							title:res.msg,
							duration: 2000,
							icon:'none'
						});
					}
				})
			},
			wxLogin(){
			  // #ifdef MP-WEIXIN
				var that = this
				uni.getUserProfile({
					desc: '登录',
					success: (res) => {
						that.userInfo.iv = res.iv
						that.userInfo.encryptedData = res.encryptedData
						let code = that.getCode()
						if(code){
							that.userInfo.code = code
							that.wechatlogin()
						}else{
							uni.login({
								success: res => {
									that.loadModal = true
									that.userInfo.code = res.code
									var storage = {
										'code':that.userInfo.code,
										'time': (new Date()).getTime(),
									}
									uni.setStorageSync('wechatcode', storage)
									that.wechatlogin()
								}
							});
						}
					},
					fail: (res) => {
						return uni.showToast({
							title:"授权微信信息异常"+res,
							duration: 2000,
							icon:'none'
						});  
					}
				});
				//#endif
			},
			getCode(){
				const info = uni.getStorageSync('wechatcode');
					if (info){
						var before = new Date().getTime() - 5 * 3600;
						if (before < info.time){
							return info.code
						}else{
							uni.removeStorageSync('wechatcode')
							return false
						}
					}
					else{
						return false
					}
			},
			
			wechatlogin(){
				let that = this
				wechatAuth(that.userInfo).then(res => {
					if(res.code == 200){
						uni.showToast({
							title:res.msg,
							duration: 2000,
							icon:'none',
							success() {
								that.getEnterprise()
							}
						});
					}
					else{
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				});
			},
			
			goto(tourl) {
				uni.navigateTo({
					url: tourl
				});
			},
		},
	}
	
</script>

<style lang="scss">
	.main{
		.outlogin{
			background: red;
			color: #fff;
			text-align: center;
			position: absolute;
			bottom: 120rpx;
			right: 20rpx;
			border-radius: 12rpx;
			padding: 8rpx 20rpx;
			z-index: 99;
			font-size: 24rpx;
		}
		.main_resume_head{
			width: 100%;
			background-color: #fff;
			display: flex;
			justify-content: center;
			image{
				width: 750rpx;
				height: 750rpx;
			}
		}
		.main_resume_content{
			background-color: #fff;
			width: 670rpx;
			margin-top: -100rpx;
			margin-bottom: 140rpx;
			border-top-left-radius: 40rpx;
			border-top-right-radius: 40rpx;
			padding: 80rpx 40rpx;
			position: absolute;
			display: flex;
			.main_resume_content_left{
				margin-right: 20rpx;
				margin-top: 40rpx;
			}
			.main_resume_content_right{	
				width: 100%;
				.main_resume_title{
					color: #000;
					font-size: 42rpx;
					font-weight: bold;
				}
				.main_resume_box{
					display: flex;
					
					.main_box_label{
						border:1px solid #666666;
						border-radius: 20rpx;
						padding: 6rpx 20rpx;
						margin-left: 10rpx;
						margin-bottom: 10rpx;
					}
					.main_resume_box_job_left{
						margin-top: 44rpx;
						margin-right: 20rpx;
					}
					.main_resume_box_job{
						margin-top: 30rpx;
						width: 100%;
						.main_resume_box_job_company{
							color: #000;
							height: 80rpx;
							line-height: 80rpx;
						}
						.main_resume_box_job_time{
							display: flex;
							align-items: center;
						}
						.main_resume_box_job_time, .main_resume_box_job_intro, .main_resume_box_job_post{
							margin: 6rpx 0;
							color: #777777;
						}
						.main_resume_box_job_time, .main_resume_box_job_post{
							color: #777777;
						}
						.main_resume_box_job_project{
							margin: 20rpx 0;
							background-color: #f9f9f9;
							border: 1px solid #cfcfcf;
							padding: 20rpx 30rpx;
							border-radius: 10rpx;
						}
					}
				}
			}
		}
		
		.main_resume_foot{
			width: 100%;
			display: flex;
			align-items: center;
			color: #fff;
			position: fixed;
			bottom: 0;
			.foot_left{
				height:120rpx;
				display: flex;
				flex-wrap: wrap;
				justify-content: center;
				background-color: #795fff;
				width: 50%;
				padding-top: 20rpx;
				text{
					height: 50rpx;
					line-height: 50rpx;
					text-align: center;
					display: block;
					width: 100%;
				}
			}
			.foot_right{
				text-align: center;
				height:140rpx;
				line-height: 140rpx;
				width: 50%;
				background-color: #000;
			}
		}
	}
</style>